//
//  LZFAdController.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/24.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFAdController.h"
#import "LZFTabBarController.h"
#import "LZFAdItem.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>

#define iPhone3 screenH==480
#define iPhone5 screenH==568
#define iPhone6 screenH==667
#define iPhone6p screenH==736

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
@interface LZFAdController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *adView;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property(nonatomic,weak)NSTimer *timer;
@property(nonatomic,strong)UIImageView *adImageView;
@property(nonatomic,strong)LZFAdItem *adItem;



@end

@implementation LZFAdController

-(UIImageView *)adImageView
{
    if (_adImageView==nil) {
        _adImageView=[[UIImageView alloc]init];
        _adImageView.userInteractionEnabled=YES;
        [self.adView addSubview:_adImageView];
        UITapGestureRecognizer *tapGes=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [_adImageView addGestureRecognizer:tapGes];
    }
    return  _adImageView;
}

//tap手势触发
-(void)tap
{
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.adItem.ori_curl]]) {
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.adItem.ori_curl]];
    }
    
   
}


- (IBAction)clickJumpBtn:(id)sender {
    //切换uiwindow的根控制器
    LZFTabBarController *tabVC=[[LZFTabBarController alloc]init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController=tabVC;
    
    //销毁定时器
    [self.timer invalidate];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [self loadData];
    

    //设置启动图片
    [self setUpLaunchImage];
    
    //设置定时器
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(change) userInfo:nil repeats:YES];
    
}

-(void)change
{
    static NSInteger count =3 ;

    
    if (count==0) {
        //切换uiwindow的根控制器,相当于点了一下jumpBtn
        [self clickJumpBtn:nil];
    }
    self.jumpBtn.titleLabel.text=[NSString stringWithFormat:@"跳过(%ld)",count];
   // [self.jumpBtn sizeToFit];
    
    count--;
    
    
}

 //加载数据
-(void)loadData
{
    
    /*
    
     http://mobads.baidu.com/cpro/ui/mads.php?code2=phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam
     */
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes=[NSSet setWithObject:@"text/html"];
    
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[@"code2"]=code2;
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *ad=responseObject[@"ad"];
        
       NSMutableArray *adItemArray = [LZFAdItem mj_objectArrayWithKeyValuesArray:ad];
        LZFAdItem  *adItem =adItemArray[0];
        self.adItem=adItem;
        
        
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:adItem.w_picurl]];
        
        CGFloat adViewW=screenW;
        CGFloat adViewH=0;
        CGFloat w=adItem.w.doubleValue;
        if (w<0) return;
    
        CGFloat h=adItem.h.doubleValue;
        adViewH=screenW*h/w;
        self.adImageView.frame=CGRectMake(0, 0, adViewW, adViewH);
        
    
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
}



//设置启动图片
-(void)setUpLaunchImage
{
    UIImage *image;
    if (iPhone3) {
        image=[UIImage imageNamed:@"LaunchImage"];
    }

    else if (iPhone5)
    {
        image=[UIImage imageNamed:@"LaunchImage-568h"];
        
    }
    
    else if(iPhone6)
    {
        image=[UIImage imageNamed:@"LaunchImage-800-667h"];
        
    }
    
    else if (iPhone6p)
    {
        image=[UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }
    
    self.launchImageView.image=image;
}


@end
